<?php
namespace App\Http\Controllers;

use App\Models\Address;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

/**
 *
 * 我的地址控制器
 *
 * @author maqu
 *
 */
class AddressController extends BaseController {

    //我的地址 列表
    /**
     * @param $user_id
     * @return mixed
     */
    public function lists($user_id){

        if(!$user_id){
            return $this->args_invalid();
        }

        //获取数据
        $address = Address::where('user_id',$user_id)
            ->get();
        
        return $this->success($address->toArray());
	}
	
    //我的地址 新增
    /**
     * @param $user_id
     * @return mixed
     */

	public function add($user_id){

        $input = Input::all();
        
        $rules = array(
            'consignee'  => 'required',                //收货人姓名
            'region_id'  => 'required|min:1',          //地区id
            'address'  => 'required',                  //详细地址
            'phone_mob'  => 'required',  //手机号码
        );
        
	    $validation = Validator::make($input, $rules);
    
        if ($validation->fails()){
            return $this->failure($validation->messages());
        }

        $consignee = Input::get('consignee');
        $region_id = Input::get('region_id');
        $region_name = Input::get('region_name');
        $address = Input::get('address');
        $zipcode = Input::get('zipcode');
        //$phone_tel = Input::get('phone_tel');
        $phone_mob = Input::get('phone_mob');
        $is_default = Input::get('is_default',0);
        
        $add_count = DB::table('address')->where('user_id',$user_id)->count();
        
        if($add_count >env("MAX_ADDRESS_COUNT",5)){
            return $this->failure('您创建的地址数量已达到上限，请修改不常用地址');
        }
        
        $default_count = DB::table('address')->where('user_id',$user_id)->where('is_default',1)->count();
       
//        $province_id = DB::table('region')
//				    ->where('region_id',DB::table('region')
//				    ->where('region_id',$region_id)
//				    ->pluck('parent_id'))
//				    ->pluck('parent_id');
		
        $data = array(
            'user_id'       => $user_id,
            'consignee'     => $consignee,
            'region_id'     => $region_id,
            'region_name'   => $region_name,
            'address'       => $address,
            'zipcode'       => $zipcode,
            //'phone_tel'     => $phone_tel,
            'phone_mob'     => $phone_mob,
        	//'Provinces_id'	=> $province_id,
        	'is_default'	=> $is_default,
        );
        
        
        if($default_count == 0){
            $data['is_default'] = 1;
        }

        //清空其他默认地址
        if( $data['is_default'] == 1){
        	DB::table('address')
			 ->where('user_id', $user_id)
			 ->update(array('is_default' => 0));
        }
        
        /* 添加地址*/
        DB::table('address')->insert($data);

        return $this->success(null);
	}

    //我的地址 修改
    /**
     * @param $user_id
     * @param $addr_id
     * @return mixed
     * @throws Exception
     */
	public function edit($user_id,$addr_id){

        if(!$user_id || !$addr_id) {
            return $this->args_invalid();
        }

	    $input = Input::all();
	    $rules = array(
            'consignee'  => 'required',                //收货人姓名
            'region_id'  => 'required|min:1',          //地区id
            'address'  => 'required',                  //详细地址
            'phone_mob'  => 'required',  //手机号码
        );
        
	    $validation = Validator::make($input, $rules);

        $consignee = Input::get('consignee');
        $region_id = Input::get('region_id');
        $region_name = Input::get('region_name');
        $address = Input::get('address');
        $zipcode = Input::get('zipcode');
        //$phone_tel = Input::get('phone_tel');
        $phone_mob = Input::get('phone_mob');
        $is_default = Input::get('is_default',0);
        
	    try {
//            /* 更新我的地址 */
//	    	  $province_id = DB::table('region')
//				    ->where('region_id',DB::table('region')
//				    ->where('region_id',$region_id)
//				    ->pluck('parent_id'))
//				    ->pluck('parent_id');

            //清空其他默认地址
			if( $is_default == 1){
	        	DB::table('address')
				 ->where('user_id', $user_id)
				 ->update(array('is_default' => 0));
	        }
	        
            DB::table('address')
                ->where('addr_id',$addr_id)
                ->where('user_id',$user_id)
                ->update(
                    array(
                        'consignee'     => $consignee,
                        'region_id'     => $region_id,
                        'region_name'   => $region_name,
                        'address'       => $address,
                        'zipcode'       => $zipcode,
                        //'phone_tel'     => $phone_tel,
                        'phone_mob'     => $phone_mob,
                    	//'Provinces_id'	=> $province_id,
                    	'is_default'	=> $is_default,
                        )
                    );

            return $this->success(null);
        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }  
	}

    //我的地址 删除
    /**
     * @param $user_id
     * @param $addr_id
     * @return mixed
     */
	public function delete($user_id,$addr_id){


        if(!$user_id || !$addr_id) {
            return $this->args_invalid();
        }

	    DB::table('address')
            ->where('user_id',$user_id)
            ->where('addr_id',$addr_id)->delete();

        return $this->success(null);
	}	

    //设为默认
    /**
     * @param $user_id
     * @param $addr_id
     * @return mixed
     */
	public function setDefault($user_id,$addr_id){

	    if(!$user_id || !$addr_id) {
	        return $this->args_invalid();
	    }

	    $default_add = DB::table('address')
	        ->where('user_id',$user_id)
	        ->where('is_default',1)->first();
	    
	    if($default_add){
	        $data['is_default'] = 0;
	        DB::table('address')
	           ->where('addr_id',$default_add->addr_id)
	           ->update($data);
	    }
	    
	    DB::table('address')
	        ->where('user_id',$user_id)
            ->where('addr_id',$addr_id)->update(
                array(
                    'is_default'=>1
                ));
	    
	    return $this->success(null);
	}
	
	//获取默认
	public function getDefault($user_id){


	    $default_add = Address::where('user_id',$user_id)
	    ->where('is_default',1)->first();

	    if(!$default_add){
	        return $this->failure('默认地址不存在');
	    }

	    return $this->success($default_add->toArray());
	}

}
